<template lang="html">
    <div class="container">
        <div class="search">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                    <el-select v-model="selectValue" @change="onSubmit" placeholder="请选择">
                      <el-option label="全部" value=""></el-option>
                      <el-option label="平台端" value="1"></el-option>
                      <el-option label="测量师端" value="2"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-input class="form_input" v-model="searchText" placeholder="请输入员工姓名或手机号"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :disabled="loading" size="small" @click="onSubmit">搜索</el-button>
                    <el-button type="primary" size="small" @click="onAddtion">新增</el-button>
                    <el-button type="danger" :disabled="isDisabled" size="small" @click="onBatch">批量删除</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="tables" v-loading="loading">
          <el-table :data="tableData" :row-class-name="rowStyle" @selection-change="handleSelectionChange" border :header-cell-style="{background:'#f5f5f5',color:'#666'}" style="width: 100%">
                <el-table-column type="selection" width="40"></el-table-column>
                <el-table-column prop="date" label="员工端" align="center">
                  <template slot-scope="scope">
                    <p v-if="scope.row.userType ==1">平台端</p>
                    <p v-else>测量师端</p>
                  </template>
                </el-table-column>
                <el-table-column prop="userName" label="员工账号" align="center"></el-table-column>
                <el-table-column prop="roleName" label="角色类型" align="center"></el-table-column>
                <el-table-column prop="realName" label="员工姓名" align="center"></el-table-column>
                <el-table-column prop="phone" label="员工手机号" align="center"></el-table-column>
                <el-table-column label="状态" align="center" width="100">
                  <template slot-scope="scope">
                      <el-switch @change="onSwitch(scope.row)" v-model="scope.row.opState" :active-value="1" :inactive-value="2" active-color="#4196FF" inactive-color="#D7D7D7"></el-switch>
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="300">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="fEdit(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="fDelete(scope.row)">删除</el-button>
                        <el-button type="warning" size="mini" @click="fDataTransfer(scope.row)">数据转移</el-button>
                    </template>
                </el-table-column>
          </el-table>
          <pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :currentPage="currentPage" :totalPage="totalPage" />
        </div>


        <el-dialog title="转移到" v-loading="loading" :visible.sync="dialogVisible" :append-to-body="true" width="35%">
              <div class="audit_dialog">
                  <el-radio v-for="(item,index) in account" :key="index" v-model="radio" :label="item.id">{{item.realName}}</el-radio>
              </div>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirm">确 定</el-button>
              </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
  data(){
    return {
      loading:false,
      dialogVisible: false,
      searchText: '',
      currentPage: 1,
      totalPage: 0,
      pageSize:10,
      tableData: [],
      currentData:[],
      account: [],
      radio: null,
      currentRowData:null,
      selectValue: '',
    }
  },
  computed:{
    isDisabled:function() {
      return this.currentData.length>0?false:true;
    }
  },
  created(){
    this.selectUserPage()
  },
  methods:{
    async selectUserPage(){
      this.loading = true;
      let data = {
        query: this.searchText,
        userType: this.selectValue,
        pageSize: this.pageSize,
        pageNumber: this.currentPage
      };
      var res = await this.$http.selectUserPage(data);
        this.loading = false;
        this.tableData = res.data.rows;
        this.totalPage = res.data.total;

        this.selectDesignerList();
    },
    selectDesignerList(){
      this.$http.selectDesignerList().then(res=>{
        if(res.data.code == 0){
          this.account = res.data.data;
        }
      })
    },
    rowStyle({row,rowIndex}) {
      for(let i=0;i<this.currentData.length;i++){
        if(rowIndex == this.currentData[i].id){
          return 'row_style';
        }
      }
    },
    handleSelectionChange(e){
      this.currentData = [];
      this.currentData = this.currentData.concat(e);
    },
    onBatch(){
      let aId = [];
      this.currentData.forEach(item=>{aId.push(item.id)});
      this.$confirm('您确定要删除这些账号吗？').then(res=>{
        return this.$http.deleteUsers({ids:aId.join(',')})
      }).then(res=>{
        if(res.data.code == 0){
          this.$message.success('删除成功');
          this.selectUserPage();
        }else{
          this.$message.error(res.data.message);
        }
      }).catch(e=>0);
    },
    fDelete(e){
      this.$confirm('您确定要删除此账号吗？').then(res=>{
         this.$http.deleteUser({id:e.id}).then(res=>{
             if(res.data.code == 0){
               this.$message.success('删除成功');
               this.selectUserPage();
             }else{
               this.$message.error(res.data.message);
             }
         })
      }).catch(e=>0);
    },
    onSubmit(){
      this.handleCurrentChange(1);
    },
    onSwitch(e){
      this.$http.closeUser({id: e.id,opState:e.opState}).then(res=>{
        if(res.data.code===0){
            this.$message.success(res.data.message);
        }else{
            this.$message.error(res.data.message);
        }
      })
    },
    fDataTransfer(e){
      this.dialogVisible = true;
      this.currentRowData = e;
    },
    confirm(){
      if(!this.radio){
        this.$message.error('请选择一个要转移的账号！！！');
        return false;
      }
      this.loading = true;
      this.$http.designerDataTransfer({
        id: this.currentRowData.id,
        designerId:this.radio
      }).then(res=>{
        this.loading = false;
        if(res.data.code == 0){
          this.$message.success(res.data.message);
          this.dialogVisible = false;
          this.onSubmit();
        }else{
          this.$message.error(res.data.message);
        }
      });
    },
    onAddtion(){
      this.$router.push('/system/account/add_account')
    },
    fEdit(e){
      this.$router.push({path: '/system/account/add_account',query:{id:e.id,userType:e.userType}})
    },
    handleSizeChange(e){
      this.pageSize = e;
      this.handleCurrentChange(1);
    },
    handleCurrentChange(e){
      this.currentPage = e;
      this.selectUserPage()
    },

  }

}
</script>

<style lang="css" scoped>
.audit_dialog{
  line-height: 30px;
}
.audit_dialog .el-radio{
  width: 20%;
  margin-right: 0;
}
</style>
